
import router from './router'
import store from './store'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
const whiteList = ['/login', '/404']
// 前置守卫 -路由跳转之前会进入
router.beforeEach(async(to, from, next) => {
  // to到哪里去
  // from从哪里来
  // next-必须执行的函数-不执行页面就会卡住 pending fullfiled rejected
  // next(false)终止跳转
  // next()放行
  nProgress.start()// 开始进度条
  if (store.getters.token) {
    // 此时已经有token了
    if (to.path === '/login') {
      next('/')
      nProgress.done()// 结束进度条
    } else {
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      nProgress.done()// 结束进度条
    }
  }
})
router.afterEach(() => {
  nProgress.done()// 结束进度条
})
